package cn.bby.jtk.base.config;

import cn.bby.jtk.base.task.AsyncUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author ben
 * @since 2025/4/17 00:16
 */
@Configuration
public class CommonThreadPoolConfig {

    @Bean
    public ExecutorService executorService() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(20);
        executor.setQueueCapacity(200);
        executor.setKeepAliveSeconds(60);
        executor.setThreadNamePrefix("common-thread-pool-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.initialize();
        ExecutorService threadPoolExecutor = executor.getThreadPoolExecutor();
        AsyncUtil.initExecutorService(threadPoolExecutor);
        return threadPoolExecutor;
    }

}
